package com.csit.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.csit.bean.Pager;
import com.csit.dao.CustomerCardDao;
import com.csit.dao.ProductDao;
import com.csit.dao.SaleDao;
import com.csit.dao.SaleDetailDao;
import com.csit.entity.CustomerCard;
import com.csit.entity.DataDictionary;
import com.csit.entity.Product;
import com.csit.entity.Sale;
import com.csit.entity.SaleDetail;
import com.csit.service.SaleService;
import com.csit.util.ListAndJsonUtil;
import com.csit.util.ResponseMsgUtil;
import com.csit.util.StringUtil;
import com.csit.util.TimeUtil;

@Service
public class SaleServiceImpl extends BaseServiceImpl<Sale, Integer> implements SaleService {
	@Resource
	private SaleDao saleDao;
	@Resource
	private SaleDetailDao saleDetailDao;
	@Resource
	private ProductDao productDao;
	@Resource
	private CustomerCardDao customerCardDao;
	@Resource
	public void setBaseDao(SaleDao saleDao) {
		super.setBaseDao(saleDao);
	}
	
	public ResponseMsgUtil addSale(Sale sale, String productids, String qtys,String prices, String isFrees, String notes) {
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		if(StringUtil.isNull(productids)){
			msgUtil.setMsg("至少采购一种商品");
			return msgUtil;
		}
		if(sale.getCustomer()==null){
			msgUtil.setMsg("客户不能为空");
			return msgUtil;
		}
		if(sale.getSaleDate()==null){
			msgUtil.setMsg("销售日期不能为空");
			return msgUtil;
		}
		if(sale.getStoreHouse()==null){
			msgUtil.setMsg("出货仓库不能为空");
			return msgUtil;
		}
		String[] productidArray = productids.split(SPLIT_REGEX);
		String[] qtyArray = qtys.split(SPLIT_REGEX);
		String[] priceArray = prices.split(SPLIT_REGEX);
		String[] isFreeArray = isFrees.split(SPLIT_REGEX);
		String[] noteArray = notes.split(SPLIT_REGEX);
		
		sale.setCreateDate(new Date());
		//使用会员卡
		if(sale.getMember()!=null){
			CustomerCard customerCard = customerCardDao.getCustomerCardByCustomer(sale.getCustomer());
			if(customerCard.getAmount()-sale.getMember()<0){
				msgUtil.setMsg("卡余额不足,请先充值");
				return msgUtil;
			}
			//会员卡钱减少
			customerCard.setAmount(customerCard.getAmount()-sale.getMember());
		}
		saveEntity(sale);
		for (int i=0;i<productidArray.length;i++) {
			SaleDetail saleDetail = new SaleDetail();
			
			Product product = productDao.get(Integer.parseInt(productidArray[i]));
			
			saleDetail.setSale(sale);
			saleDetail.setProduct(product);
			saleDetail.setQuantity(Double.parseDouble(qtyArray[i]));
			
			//更新库存
			product.setCurrentQuantity(product.getCurrentQuantity()-saleDetail.getQuantity());
			
			saleDetail.setPrice(Double.parseDouble(priceArray[i]));
			saleDetail.setAmount(saleDetail.getQuantity()*saleDetail.getPrice());
			saleDetail.setPosition(i+1);
			if("1".equals(isFreeArray[i])){
				saleDetail.setIsFree(true);
				saleDetail.setAmount(0d);
			}else{
				saleDetail.setIsFree(false);
			}
			if(!"*".equals(noteArray[i])){
				saleDetail.setNote(noteArray[i].trim());
			}
			saleDetailDao.save(saleDetail);
		}
		msgUtil.setSuccess(true);
		return msgUtil;
	}

	public ResponseMsgUtil delete(Integer saleID) {
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		Sale sale = getEntity(saleID);
		//使用会员卡
		if(sale.getMember()!=null){
			CustomerCard customerCard = customerCardDao.getCustomerCardByCustomer(sale.getCustomer());
			//会员卡钱减少
			customerCard.setAmount(customerCard.getAmount()+sale.getMember());
		}
		List<SaleDetail> list = sale.getSaleDetailList();
		for(SaleDetail saleDetail : list){
			Product product = saleDetail.getProduct();
			//先更新之前的
			product.setCurrentQuantity(product.getCurrentQuantity()+saleDetail.getQuantity());
			saleDetailDao.delete(saleDetail);
		}
		deleteEntity(sale);
		msgUtil.setSuccess(true);
		return msgUtil;
	}

	public String getCode(DataDictionary shop) {
		Long count = saleDao.getCount(shop);
		String code = "S"+TimeUtil.formatDate(new Date(),"yyyyMMdd").substring(2)+String.format("%03d", count+1);
		return code;
	}

	public String querySale(Integer pageNumber, Integer pageSize, Sale sale,Date beginDate, Date endDate) {
		if(sale.getShop()==null){
			return ListAndJsonUtil.EMPTYJSON;
		}
		Pager pager = new Pager(pageNumber, pageSize);
		pager = saleDao.querySale(pager, sale, beginDate, endDate);
		String[] propertiesName = {"saleID","saleCode","saleDate","amount","discountAmount","saleer","cash","card","member",
					    "note","storeHouse.dataDictionaryName","customerName"};
		String jsonArray = ListAndJsonUtil.getJSONString(pager.getList(), propertiesName, pager.getTotalCount());
		return jsonArray;
	}

	public String getDetailList(Sale sale) {
		sale = getEntity(sale.getSaleID());
		List<SaleDetail> list = sale.getSaleDetailList();
		String[] propertiesName = {"saleDetailID","product.productName","product.productSize","product.unitName",
								   "quantity","price","amount","isFree","note"};
		String jsonArray = ListAndJsonUtil.getJSONString(list, propertiesName);
		return jsonArray;
	}

}
